<?php
include('database.php');

$token = addslashes(@$_GET['token']);
if(!empty($token)) {
    $user = checkLogin($token);
    if($user) {
        $userGroup = checkUserActive($token);
        if($userGroup['group_id']) {
            //$link = 'http://idvnn.com/website/reply/api.php?q=foody&rid='. $userGroup['group_id'];
            
            $list = getListComment('foody', $userGroup);
            $report = getReport($userGroup);
            $setting = getSetting($userGroup);
            $response = array(
                'result' => true,
                'data' => array(
                    'list' => $list,
                    'report' => $report,
                    'setting' => $setting
                )
            );
            
            echo json_encode($response);
            exit;
        } else {
            echo '{"result":"false", "msg":"User has not been activated"}';exit;
        }
    } else {
        echo '{"result":"false"}';exit;
    }
} else {
    echo '{"result":"false", "msg":"System Error"}';exit;
}

/**
* Check token is exits in database 
* @param string $email is email of user
* @return boolean true|false
*/
function checkUserActive($token)
{
    $query  = 'SELECT u.email, u.brand, g.group_id FROM `cp_users` as u ';
    $query .= 'LEFT JOIN group_foody as g ON u.id = g.user_id ';
    $query .= 'WHERE token ="' . $token . '" AND status=1';

    $result = mysql_query($query);
    $data = '';
    if ($result) {
        $data = mysql_fetch_assoc($result);
    } else {
        //Error, alert to email to see query
        //echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        echo '{"result":"false", "msg":"System Error"}';exit;
    }

    return $data;
}

/**
* Check token is exits in database 
* @param string $email is email of user
* @return boolean true|false
*/
function checkLogin($token)
{
    $query = 'SELECT * FROM `cp_users` WHERE token ="' . $token . '"';
    $result = mysql_query($query);
    $data = '';
    if (!$result) {
        //Error, alert to email to see query
        //echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        return false;
    }

    return true;
}

function getListComment($chanel, $userGroup)
{
    if($chanel == 'foody') {
        return getCommentFoody($userGroup);
    }
}

function getCommentFoody($userGroup)
{
    //$sql = "SELECT image,username,description,date_comment FROM `group_foody_comment` WHERE point <= 5 and group_id = " . $userGroup['group_id'] . " order by time DESC limit 10";
    $sql = "SELECT image,username,description,date_comment FROM `group_foody_comment` WHERE group_id = " . $userGroup['group_id'] . " order by time DESC limit 10";
        $result = mysql_query($sql);

        if (!$result) {
            echo "Could not successfully run query ($sql) from DB: " . mysql_error();
            exit;
        }
        
        $num_rows = mysql_num_rows($result);
        $data = array();
        if($num_rows > 0) {
            $i=0;
            while ($row = mysql_fetch_assoc($result)) {
                $data[$i]["image"] = $row["image"];
                $data[$i]["username"] = $row["username"];
                $data[$i]["title"] = 'Quan Nuong Ngoi - Nguyen Trai';
                $data[$i]["des"] = myTruncate($row["description"], 100);
                $data[$i]["date_comment"] = $row["date_comment"];
                
                $i++;
            }

            mysql_free_result($result);
        } else {
            //case not data
        }

        return $data;
}

function getReport($userGroup)
{
    $data = array(
        'day' => 4,
        'week' => 10,
        'month' => 20,
        'chart' => array(
            array('date' => '2015-03-09', 'value' => 2),
            array('date' => '2015-03-10', 'value' => 5),
            array('date' => '2015-03-11', 'value' => 6),
            array('date' => '2015-03-12', 'value' => 7),
            array('date' => '2015-03-13', 'value' => 1),
            array('date' => '2015-03-14', 'value' => 4),
            array('date' => '2015-03-15', 'value' => 8),
        )
    );
  
    return $data;
}
function getSetting($userGroup)
{
    $data = array(
        'notification' => 1,
        'brand' => $userGroup['brand'],
        'email' => $userGroup['email'],
    );
    
    return $data;
}

function myTruncate($string, $limit, $break=".", $pad="...")
{
  // return with no change if string is shorter than $limit
  if(strlen($string) <= $limit) return $string;

  // is $break present between $limit and the end of the string?
  if(false !== ($breakpoint = strpos($string, $break, $limit))) {
    if($breakpoint < strlen($string) - 1) {
      $string = substr($string, 0, $breakpoint) . $pad;
    }
  }

  return $string;
}